package org.grayrabbit.cms.publish.generate.factory;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.grayrabbit.cms.entity.Admin;
import org.grayrabbit.cms.entity.Site;
import org.grayrabbit.cms.publish.api.Generatable;
import org.grayrabbit.cms.publish.generate.Generater;
import org.grayrabbit.cms.publish.generate.IndexGenerater;
import org.grayrabbit.cms.publish.generate.MultiGenerater;

import freemarker.template.Configuration;

/**
 * @author sys53
 * @version 1.0
 * @created 2011-6-15 10:43:13
 */
public class IndexGeneraterFactory implements GeneraterFactory {
	private Configuration configuration;
	private List<Site> sites = new ArrayList<Site>();
	private String rootPath;
	private Generatable generatable;
	private Admin admin;

	public IndexGeneraterFactory(Configuration configuration,Site site,String rootPath,Generatable generatable,Admin admin){
		this.configuration = configuration;
		this.sites.add(site);
		this.rootPath = rootPath;
		this.generatable = generatable;
		this.admin = admin;
	}

	public IndexGeneraterFactory(Configuration configuration, List<Site> sites,String rootPath,Generatable generatable,Admin admin) {
		this.configuration = configuration;
		this.sites = sites;
		this.rootPath = rootPath;
		this.generatable = generatable;
		this.admin = admin;
	}

	public Generater create(){
		List<Generater> list = new ArrayList<Generater>();
		for (Site site : sites) {
			String path = site.getSiteIndexTpl().substring((File.separator+rootPath).length());
			list.add(new IndexGenerater(configuration,site,path,generatable,admin));
		}
		if(list.size()==1)
			return list.get(0);//单个站点时
		return new MultiGenerater(list);
	}

}